// fillings for Players header
#include "Includes.hpp" // includes for project
#include "Players.hpp" // class declarations

////////////////////////////// EXTERNS ///////////////////////////////
class Player;

////////////////////////////// CONSTRUCTOR ///////////////////////////
Players::Players() {
    playersLlist = new List(Sorted); // make sorted list
    currentuniqueid = 0; // initialize the id numbers
}

////////////////////////////// CLASS FUNCTIONS ///////////////////////
bool Players::addPlayer() {
    Player* tempplayer = new Player(currentuniqueid); // make new
    if(playersLlist->add(tempplayer)) { // player and add it to list // if this goes well,
	currentuniqueid++;                // increment id number, and return true
	return true;
    }
    return false; // if not return false
}

bool Players::addPlayer(Player* sentplayer, bool count) {
    if (playersLlist->add(sentplayer)) { // if player is added to list
	count? currentuniqueid++: NULL; // count if bool is one
	return true; // and return true
    } // if player was not added to list
    return false; // return false
}

void Players::uniqueIdInit() {
    Player* tempplayer = (Player*)playersLlist->remove_no(playersLlist->no_of_elements()); // remove the last
    // player in the list
    currentuniqueid = (tempplayer->getNumber())+1; // set current unique id to its actual value
    playersLlist->add(tempplayer); // add player back into list
}

void Players::printAllPlayers() {
    playersLlist->display_list(); // print all members of players list
}

void Players::printPlayer(int sentnum) {
    playersLlist->display_element(sentnum); // print player with 'sentnum'
}

void Players::printPlayer(char* senttext) {
    int tempnum; // make temporary int to work on
    tempnum = this->getPlayer(senttext); // find player and assign number
    playersLlist->display_element(tempnum); // print player with 'tempnum' to screen
}

int Players::getPlayer(char* searchedstring, bool nameoraddr) {
// searches for name by default
    int retptr; // int pointer  returning number
    Player* tempplayer; // playerfor pointer to work on and return
    for (int pvar = 0; pvar <= playersLlist->no_of_elements(); pvar++) { // go through list
	tempplayer = (Player*)playersLlist->remove_no(pvar); // pull out one
	if (strcmp(searchedstring, (nameoraddr ? tempplayer->getAddress() : tempplayer->getName())) == 0)
	{ // if 'nameoraddr' is true compare 'searchedstring'
	    // to address, if false compare to
	    // name. if these are equal
	    retptr = (*tempplayer).getNumber(); // return int pointer
	    playersLlist->add(tempplayer); // add player back into list
	    return retptr; // return address of found
	} // if (strcmp()) end
    } // for (pvar) end
    cout << "\ncould not find player with " // if no one is found/this point is reached
	 << (nameoraddr ? ("address ") : ("name ")) // print, depending on the bool
	 << searchedstring; // an error with searched for address/name
    return NULL; // if nothing was found, return NULL
}

int Players::getPlayer(int playernum) {
    int retptr; // int pointer for returning correct number
    Player* tempplayer; // player pointer to return
    if (playersLlist->in_list(playernum)) { // if the one we are looking for is
	tempplayer = (Player*)playersLlist->remove(playernum); //  in list // pull him out
	retptr = (*tempplayer).getNumber(); // point returning int pointer to players number
	playersLlist->add(tempplayer); // put player back into list
	return retptr; // return int pointer
    } else { // if player we are looking for is !in list
	cout << "\ncould not find player with number " << playernum; // print error
    } // go back
    return NULL;
}


bool Players::deletePlayer(int playernum) {
    if(playersLlist->in_list(playernum)) {   //If playernum is in the list, 
	playersLlist->destroy(playernum); // destroy the player
    }
    else {
	cout << "\nCould not find player with number " << playernum; //Print error
    }
    return NULL;
}


int* Players::deletePlayer(char* searchedstring, bool nameoraddr) {
// searches for name by default
    Player* tempplayer; // player pointer to work on and return
    for (int pvar = 0; pvar <= playersLlist->no_of_elements(); pvar++) { // go through list
	tempplayer = (Player*)playersLlist->remove_no(pvar); // pull out one
	if (strcmp(searchedstring, (nameoraddr ? tempplayer->getAddress() : tempplayer->getName())) == 0)
	{ // if 'nameoraddr' is true compare 'searchedstring'
	    // to address, if false compare to
	    // name. if these are equal
	    delete tempplayer; // delete the player object
	    break; // stop looping
	} else { // if they are not equal
	    playersLlist->add(tempplayer); // put 'tempplayer' back into list 
	} // if (strcmp()) end
    } // for (pvar) end
    cout << "\ncould not find player with " // if no one is found/this point is reached
	 << (nameoraddr ? ("address ") : ("name ")) // print, depending on the bool
	 << searchedstring; // an error with searched for address/name
    return NULL; // if nothing was found, return NULL
}

Players::~Players() {
    for (int lvar = playersLlist->no_of_elements(); lvar > 0; lvar--) { // go through list
	playersLlist->destroy(lvar); // destroy one, then another one -
    } // for lvar end
    delete playersLlist; // at last, delete the entire list
}
